//
//  StockFundFlowChar.m
//  Stock
//
//  Created by sun black on 11-8-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StockFundFlowChar.h"

@implementation StockFundFlowChar
@synthesize fundFlow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.graph.plotAreaFrame.paddingLeft = 0.0f;
    self.graph.plotAreaFrame.paddingBottom = 0.0f;
    
}
-(void)loadChartData{
    [self.loadingView removeFromSuperview];
    self.loadingView = nil;
    float minAmount=0,maxAmount=0;
    float f0 = [fundFlow.f0 floatValue];
    float f1 = [fundFlow.f1 floatValue];
    float f2 = [fundFlow.f2 floatValue];
    float f3 = [fundFlow.f3 floatValue];
    minAmount = MIN(f3,MIN(f2, MIN(f1, MIN(f0, 0))));
    maxAmount = MAX(f3,MAX(f2, MAX(f1, MAX(f0, 0))));
    [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"amount",[NSNumber numberWithInt:1],@"xValue", nil]];
    [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"amount",[NSNumber numberWithInt:3],@"xValue", nil]];
    [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"amount",[NSNumber numberWithInt:5],@"xValue", nil]];
    [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"0",@"amount",[NSNumber numberWithInt:7],@"xValue", nil]];
    
    if (f0>0) {
        [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f0],@"amount",[NSNumber numberWithInt:2],@"xValue", nil]];
    }else{
        [downDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f0],@"amount",[NSNumber numberWithInt:2],@"xValue", nil]];

    }
    
    if (f1>0) {
        [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f1],@"amount",[NSNumber numberWithInt:4],@"xValue", nil]];
    }else{
        [downDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f1],@"amount",[NSNumber numberWithInt:4],@"xValue", nil]];
        
    }
    if (f2>0) {
        [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f2],@"amount",[NSNumber numberWithInt:6],@"xValue", nil]];
    }else{
        [downDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f2],@"amount",[NSNumber numberWithInt:6],@"xValue", nil]];
        
    }
    if (f3>0) {
        [upDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f3],@"amount",[NSNumber numberWithInt:8],@"xValue", nil]];
    }else{
        [downDatas addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",f3],@"amount",[NSNumber numberWithInt:8],@"xValue", nil]];
        
    }
    
    [self buildChartView];
    
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(minAmount) length:CPTDecimalFromInt(maxAmount-minAmount)];
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0f) length:CPTDecimalFromInteger(8)];
    
    
    
    //坐标
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
    axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromInt(minAmount-100);
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromInt(2);
    axisSet.xAxis.minorTicksPerInterval = 0;
    NSMutableArray *labels = [NSMutableArray array];
    NSArray *titles = [NSArray arrayWithObjects:@"特大单",@"大单",@"小单",@"散弹",nil];
    for (int i=1; i<6; i++) {
        NSString *title = @"v";
        if ([titles count]>0 && i<[titles count]-1) {
            title = [titles objectAtIndex:i];
        }
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:title textStyle:axisSet.yAxis.labelTextStyle];
        NSNumber *tickLocation = [NSDecimalNumber numberWithInt:i];
		newLabel.tickLocation = [tickLocation decimalValue];
		[labels addObject:newLabel];
		[newLabel release];
    }
	axisSet.xAxis.axisLabels =  [NSSet setWithArray:labels];
    
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(maxAmount-minAmount+1000.0);
    axisSet.yAxis.minorTicksPerInterval = 0;
    axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
    
    
    [self.graph reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

@end
